package com.chwn.curiousdiary.ui.story.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;

import com.chwn.curiousdiary.R;
import com.chwn.curiousdiary.adapter.StoryAdapter;
import com.chwn.curiousdiary.bean.StoryBean;
import com.chwn.curiousdiary.gloable.Constant;
import com.chwn.curiousdiary.service.AudioService;
import com.chwn.curiousdiary.ui.story.contract.NewCommendContract;
import com.chwn.curiousdiary.ui.story.presenter.NewCommendPresenter;
import com.chwn.curiousdiary.ui.play.activity.StoryPlayActivity;
import com.chwn.harulib.adapter.ViewHolder;
import com.chwn.harulib.adapter.interfaces.OnItemClickListener;
import com.chwn.harulib.mvp.base.BaseMVPFragment;
import com.chwn.harulib.mvp.rx.HttpResultException;
import com.chwn.harulib.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Myshikin on 2017/3/20.
 */

public class NewCommendFragment extends BaseMVPFragment<NewCommendPresenter> implements NewCommendContract.View, XRecyclerView.LoadingListener {

    @BindView(R.id.xrv_content)
    XRecyclerView mContentRV;

    private StoryAdapter mCommendAdapter;

    private boolean isRefresh = true;

    /**
     * 当前加载页数
     */
    private int page = 1;

    /**
     * 故事分类 默认为最新页面的数据
     */
    private String storyPackageId = "";

    private String mStoryListTitle;

    @Override
    protected int setContentViewId() {
        return R.layout.fragment_new_commend;
    }

    @Override
    protected void initData() {
        super.initData();
        Bundle arguments = getArguments();
        if (arguments != null) {
            storyPackageId = arguments.getString(Constant.STORY_TYPE);
            mStoryListTitle = arguments.getString(Constant.STORY_LIST);
        }
    }

    @Override
    protected void initContentView() {
        ButterKnife.bind(this, mInflateContentView);
        mCommendAdapter = new StoryAdapter(mActivity, null, true);
        mContentRV.setLayoutManager(new LinearLayoutManager(mActivity));
        mContentRV.setLoadingListener(this);
        mContentRV.setLoadingMoreEnabled(true);
        mContentRV.setAdapter(mCommendAdapter);

        initStateViewData();
    }

    private void initStateViewData() {
        setEmptyView(R.drawable.icon_no_data);
        setErrorView(R.drawable.icon_no_data);
        setErrorText("哎呀，快去检查一下网络吧～");
        setEmptyText("没有故事～");
    }

    @Override
    protected void initListener() {
        super.initListener();
        mCommendAdapter.setOnItemClickListener(new OnItemClickListener<StoryBean>() {
            @Override
            public void onItemClick(ViewHolder viewHolder, StoryBean data, int position) {
                onCommendItemClick(data, position);
            }
        });
    }

    /**
     * 条目点击事件
     * @param data
     * @param position
     */
    private void onCommendItemClick(StoryBean data, int position) {
        ArrayList<StoryBean> list = (ArrayList<StoryBean>) mCommendAdapter.getDatas();

        Intent intent = new Intent(getActivity(), StoryPlayActivity.class);
        String packageId = storyPackageId;
        if (TextUtils.isEmpty(packageId)) {
            packageId = "newest";
        }
        intent.putExtra(Constant.PACKAGE_ID, packageId);
        intent.putExtra("list", list);
        intent.putExtra("position", position);
        startActivity(intent);
    }


    @Override
    public void onRequestError(Throwable e) {
        super.onRequestError(e);
        if (!isRefresh && e instanceof HttpResultException) {
            mContentRV.loadMoreComplete();
            mContentRV.refreshComplete();
            mContentRV.noMoreLoading();
            page--;
        } else if (e instanceof HttpResultException) {
            mContentRV.noMoreLoading();
            if (mCommendAdapter.getDatas() == null || mCommendAdapter.getDatas().size() < 1) {
                showEmptyView();
            }
        } else {
            if (mCommendAdapter.getDatas() == null || mCommendAdapter.getDatas().size() < 1) {
                showErrorView();
            }
        }
    }

    @Override
    protected void getData() {
        mPresenter.getNewCommendList(storyPackageId, page);
    }


    @Override
    public void showNewCommendList(List<StoryBean> storyList) {
        showContentView();
        if (storyList.size() < Constant.PAGE_COUNT) {
            mContentRV.noMoreLoading();
        }
        if (isRefresh) {
            mCommendAdapter.setNewData(storyList);
            mContentRV.refreshComplete();
        } else {
            mCommendAdapter.setLoadMoreData(storyList);
            mContentRV.loadMoreComplete();
        }
    }

    @Override
    public void onRefresh() {
        isRefresh = true;
        page = 1;
        getData();
    }

    @Override
    public void onLoadMore() {
        isRefresh = false;
        page++;
        getData();
    }



    /**
     * 跳转至播放界面
     */
    private void enterPlayPage(int position) {
        //进入播放器 --- 判断网络连接是否正常
        ArrayList<StoryBean> list = (ArrayList<StoryBean>) mCommendAdapter.getDatas();

        Intent intent = new Intent(getActivity(), StoryPlayActivity.class);
        intent.putExtra(Constant.STORY_SERVICE_TYPE, AudioService.NEWEST_ENTER);
        String packageId = storyPackageId;
        if (TextUtils.isEmpty(packageId)) {
            packageId = "newest";
        }
        intent.putExtra(Constant.PACKAGE_ID, packageId);
        intent.putExtra("list", list);
        intent.putExtra("position", position);
        startActivity(intent);
    }
}
